﻿using Kinlo.Entitys;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using XCode.Membership;
using XingLucifer.IBase.Enums;

namespace Kinlo.MESDocking
{
    /// <summary>
    /// 设备状态上传接口
    /// </summary>
    public class UploadMachineStatus : MESInteractionBase
    {
        public UploadMachineStatus(Action<string, string, string, string, string, long, DateTime, string, string, string,string> log, Action<string, string, string> dialog)
            :base(log, dialog, "设备状态上传")
        {

        }
        public ResultTypes Interaction(DateTime dateTime, ProductionData[] production, Dictionary<string, object> parameters)
        {
            bool is_mes_enable = (bool)parameters["mes_enable"];
            if (is_mes_enable)
            {
                _stopwatch.Restart();
                string json = JsonSerializer.Serialize(parameters, HttpHelper.Options);
                var result = Post(json, (string)parameters["url"], (int)parameters["outtime"], (int)parameters["reconnect_count"]);
                _stopwatch.Stop();
                _log?.Invoke("", _name, result.Item2, json, result.Item1 ? "请求成功" : "请求异常",
                    _stopwatch.ElapsedMilliseconds, dateTime, parameters["machineNo"].ToString(), parameters["producttype"].ToString(), "uploadMachineStatus", (string)parameters["url"]);
                Dictionary<string, object> result_json = JsonSerializer.Deserialize<Dictionary<string,object>>(json, HttpHelper.Options);
                if ((bool)result_json["status"] == false)
                {
                    _dialog?.Invoke(_name, result_json["errorCode"].ToString(), result_json["message"].ToString());
                }
                return result.Item1 ? ResultTypes.合格 : ResultTypes.MES_NG;
            }
            return ResultTypes.合格;
        }
    }
}
